const request = require('request')

const appid = 'wxac71dbacdc2987ab';
const secret = '9ab60af3eeb0b3f355b0e5881d98d4ea';

const authorizationCode = 'test';
let sessionKey = null;
let openid = null;

const login = async code => {

  return new Promise(function (resolve, reject) {
    request(`https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${code}&grant_type=${authorizationCode}`, (err, response, body) => {
      if (err) reject(err);
      const data = JSON.parse(body);
      // console.log("response",JSON.parse(response));
      // console.log("data",data);
      /*
        签名校验以及数据加解密涉及用户的会话密钥session_key。 需要保存在服务器
        openid 判断是否是同一个用户
        session_key 判断用户是否失效
  
        data: {
          openid: '**********',
          session_key: '********'
        }
      */
      console.log("data", data);
      resolve(data)
    })
  })

}


module.exports = { login, sessionKey, openid }